package com.kawa.controller;

import com.kawa.pojo.User;
import com.kawa.service.RibbonService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    private RibbonService ribbonService;

    @GetMapping("/getUser/{id}")
    @HystrixCommand(fallbackMethod = "getUserByIdError")
    public String getUserById(@PathVariable Long id){
        System.out.println("服务消费者调用提供者 根据ID查询用户姓名！");
        return  ribbonService.getUserbyId(id);
    }

    //2.备选方案  用于服务熔断时进行服务降级
    public String getUserByIdError(@PathVariable Long id){
        System.out.println(" 进入服务熔断处理---- 服务提供方执行根据id查询用户姓名");
        return "当前用户不存在";
    }

    @GetMapping("/addUser")
    public User add(User user){
        System.out.println("服务消费者调用提供者 添加用户！");
        return ribbonService.add(user);
    }
}
